%typemap(in) ap::real_1d_array {
    $1 = new ap::real_1d_array();
    $1->setlength(RARRAY_LEN($input));
    for(long i=0;i<RARRAY_LEN($input);i++) {
        $1->operator()(i)=rb_num2dbl(RARRAY_PTR($input)[i]);
    }
}


%apply  ap::real_1d_array {ap::real_1d_array const &};

namespace ap {
/********************************************************************
Template of a dynamical one-dimensional array
********************************************************************/
template<class T, bool Aligned = false>
class template_1d_array
{
public:
    template_1d_array(); 
    ~template_1d_array();
    template_1d_array(const template_1d_array &rhs);
    %extend {
        T __getitem__(int i) {
            return $self->operator()(i);
        };
        T __setitem__(int i, T v) { 
            return $self->operator()(i)=v;
        };
        int size() { 
            return $self->gethighbound()-$self->getlowbound()+1;
        }
    }
    
    void setbounds( int iLow, int iHigh );
    void setlength(int iLen);
    void setcontent( int iLow, int iHigh, const T *pContent );
    T* getcontent();
    int getlowbound(int iBoundNum = 0);
    int gethighbound(int iBoundNum = 0);
};

template<class T, bool Aligned = false>
class template_2d_array
{
public:
    template_2d_array();

    ~template_2d_array();

    template_2d_array(const template_2d_array &rhs);
    T& operator()(int i1, int i2);
    
    void setbounds( int iLow1, int iHigh1, int iLow2, int iHigh2 );

    void setlength(int iLen1, int iLen2);

    void setcontent( int iLow1, int iHigh1, int iLow2, int iHigh2, const T *pContent );

    int getlowbound(int iBoundNum) const;

    int gethighbound(int iBoundNum) const;
    
    %extend {
    double __getitem__(int x,int y) {
        return $self->operator()(x,y);
    }
    void __setitem__(int x,int y, T val) {
        $self->operator()(x,y)=val;
    }

}
    
};



typedef template_1d_array<int>          integer_1d_array;
typedef template_1d_array<double,true>  real_1d_array;
typedef template_1d_array<complex>      complex_1d_array;
typedef template_1d_array<bool>         boolean_1d_array;

typedef template_2d_array<int>          integer_2d_array;
typedef template_2d_array<double,true>  real_2d_array;
typedef template_2d_array<complex>      complex_2d_array;
typedef template_2d_array<bool>         boolean_2d_array;

%template(Integer1dArray) template_1d_array<int>;
%template(Real1dArray) template_1d_array<double,true>;
%template(Boolean1dArray) template_1d_array<bool>;
%template(Integer2dArray) template_2d_array<int>;
%template(Real2dArray) template_2d_array<double,true>;
};


VALUE real1d_to_array(ap::real_1d_array *x);
void array_to_real1d(VALUE ary, ap::real_1d_array *x);
